//
//  STData.swift
//  Demo-04-TabBarTest
//
//  Created by 苏统世 on 16/5/15.
//  Copyright © 2016年 SuTon. All rights reserved.
//

import UIKit

class STData: NSObject {

    
    /// 缓存cell高
    var cellHeight:CGFloat?
    
    /// 微博创建时间
    var created_at: String?
    /// 微博ID
    var id: Int64 = 0
    /// 微博信息内容
    var text: String?
    /// 微博来源
    var source: String?{
        didSet{
            /*  Optional("<a href=\"http://weibo.com/\" rel=\"nofollow\">微博 weibo.com</a>")  */
            if source==nil || source?.characters.count==0{
//                STPrint(items: "微博来源没有数据")
                return
            }
//            STPrint(items: "source==>\(source)")
            //截取字符串
            let sourceString = source! as NSString
            let range1 = sourceString.rangeOfString(">")
//            STPrint(items: "range1==>\(range1)")
            let range2 = sourceString.rangeOfString("</")
//            STPrint(items: "range2==>\(range2)")
            let range = NSRange(location: range1.location+1, length: range2.location-1-range1.location)
//            STPrint(items: "source==>\(sourceString.substringWithRange(range))")
            /// 内部已做处理，不会递归
            source=sourceString.substringWithRange(range)
        }
    }
    /// 转发数
    var reposts_count: Int = 0
    /// 评论数
    var comments_count: Int = 0
    /// 表态数
    var attitudes_count: Int = 0
    /// 包含有 user 的字典信息
    var user:STUser?
    /// 转发微博模型
    var retweeted_status:STData?
    /*
    [
        {"thumbnail_pic":"http://ww2.sinaimg.cn/thumbnail/e9492957gw1f3wde5ykd3j20go0gomyp.jpg"},
        {"thumbnail_pic":"http://ww2.sinaimg.cn/thumbnail/e9492957gw1f3wde7z6u8j20go0gigo2.jpg"},
    ]
    */
    /// 微博配图: 数组里面是字典 // Int类型的数组[Int], 字典类型的数组 [String: AnyObject] -> [[String: AnyObject]]
    var pic_urls: [[String: AnyObject]]?{
        
        didSet{
            guard let icon_urls=pic_urls else{
                STPrint(items: "pic_urls 还没有值")
                return
            }
            for dict in icon_urls{
                pictureArray.append(NSURL(string: dict["thumbnail_pic"] as! String)!)
            }
        }
    }
    /// 抽取数组字典中的微博配图，加入到微博配图数组，方便使用
    var pictureArray=[NSURL]()
    
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    /// 字典中的key在模型中找不到对应的属性
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user"{
            if let dict = value as? [String:AnyObject]{
                self.user = STUser(dict: dict)
            }
            //避免给super.setValue:forKey:重新覆盖成字典
            return
        }
        if key == "retweeted_status"{
            if let dict = value as? [String:AnyObject]{
                retweeted_status = STData(dict: dict)
            }
            return
        }
        super.setValue(value, forKey: key)
    }
    
}
